﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace QuanLyHocSinh.GUI
{
    public partial class F_QuanLyLopHoc : DevExpress.XtraEditors.XtraUserControl
    {
        DTOLop selected_Lop;
        public F_QuanLyLopHoc()
        {
            InitializeComponent();
        }
<<<<<<< .mine
=======

        private void F_QuanLyLopHoc_Load(object sender, EventArgs e)
        {
            selected_Lop = new DTOLop();
            DataTable khoilop = BUSQuanLyMonHoc.KhoiLop();
            this.lkeKhoiLopF.Properties.DataSource = khoilop.DefaultView;
            this.lkeKhoiLopF.Properties.DisplayMember = "TENKHOILOP";
            this.lkeKhoiLopF.Properties.ValueMember = "MAKHOILOP";
            this.lkeKhoiLop.Properties.DataSource = khoilop.DefaultView;
            this.lkeKhoiLop.Properties.DisplayMember = "TENKHOILOP";
            this.lkeKhoiLop.Properties.ValueMember = "MAKHOILOP";
            LoadDataGridView();
        }

        private void LoadDataGridView()
        {
            DataTable datadulieu = BUSQuanLyMonHoc.LietKeDanhSachLopHocByKhoiLop("");
            this.grDuLieu.DataSource = datadulieu.DefaultView;
        }

        private void lkeKhoiLopF_Properties_EditValueChanged(object sender, EventArgs e)
        {
            string khoilop = (string)this.lkeKhoiLopF.EditValue;
            if (this.lkeKhoiLopF.EditValue != null)
            {
                DataTable lophoc = BUSQuanLyMonHoc.LietKeDanhSachLopHocByKhoiLop(khoilop);
                this.lkeTenLop.Properties.DataSource = lophoc.DefaultView;
                this.lkeTenLop.Properties.DisplayMember = "TENLOP";
                this.lkeTenLop.Properties.ValueMember = "MALOP";
            }
            else
                this.lkeTenLop.EditValue = null;
        }
>>>>>>> .r57

        private void btnXoaThongTin_Click(object sender, EventArgs e)
        {
            this.lkeKhoiLopF.EditValue = null;
            LoadDataGridView();
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            DataTable dtable = BUSQuanLyMonHoc.LietKeDanhSachLopHocByMaLop(this.lkeTenLop.EditValue);
            this.grDuLieu.DataSource = dtable.DefaultView;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.btnThem.Text == "Thêm")
                {
                    xoaTrangCacThuocTinh();
                    setCacThuocTinh(true);
                    this.btnThem.Text = "Lưu";
                    this.btnBoQua.Enabled = true;
                    this.btnSua.Enabled = this.btnXoa.Enabled = false;
                }
                else
                {
                    DTOLop lop = new DTOLop();
                    lop.TenLop = this.txtTenLop.Text;
                    lop.SiSo = int.Parse(this.txtSiSo.Text);
                    lop.MaKhoiLop = this.lkeKhoiLop.EditValue.ToString();
                    BUSQuanLyMonHoc.ThemLopHoc(lop);
                    LoadDataGridView();
                    this.btnThem.Text = "Thêm";
                    setCacThuocTinh(false);
                    this.btnSua.Enabled = this.btnXoa.Enabled = false;
                    this.btnBoQua.Enabled = false;
                    setCacThuocTinh(false);
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void setCacThuocTinh(bool p)
        {
            this.txtSiSo.Enabled = p;
            this.txtTenLop.Enabled = p;
            lkeKhoiLop.Enabled = p;
        }

        private void xoaTrangCacThuocTinh()
        {
            this.txtTenLop.Text = "";
            this.txtSiSo.Text = "";
            this.lkeKhoiLop.EditValue = null;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            BUSQuanLyMonHoc.XoaLopHoc(selected_Lop);
            LoadDataGridView();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.btnSua.Text.Equals("Sửa"))
                {
                    setCacThuocTinh(true);
                    this.btnSua.Text = "Lưu";
                    this.btnThem.Enabled = this.btnXoa.Enabled = false;
                    this.btnBoQua.Enabled = true;
                }
                else
                {
                    selected_Lop.TenLop = this.txtTenLop.Text;
                    selected_Lop.SiSo = int.Parse(this.txtSiSo.Text);
                    selected_Lop.MaKhoiLop = this.lkeKhoiLop.EditValue.ToString();
                    BUSQuanLyMonHoc.SuaLopHoc(selected_Lop);
                    LoadDataGridView();
                    this.btnSua.Text = "Sửa";
                    this.btnSua.Enabled = this.btnXoa.Enabled = false;
                    this.btnThem.Enabled = true;
                    setCacThuocTinh(false);
                    this.btnBoQua.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void dgvLopHoc_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            int[] select = dgvLopHoc.GetSelectedRows();
            if (select.Length > 0)
            {
                DataRow row = (DataRow)dgvLopHoc.GetFocusedDataRow();
                if (row != null)
                {
                    setDuLieuTextBox(row);
                    selected_Lop.MaLop = row[0].ToString();//
                    selected_Lop.MaKhoiLop = row[3].ToString();
                    selected_Lop.TenLop = row[1].ToString();
                    selected_Lop.SiSo = int.Parse(row[2].ToString());
                    this.btnXoa.Enabled = true;
                    this.btnSua.Enabled = true;
                }
            }
            else
            {
                this.btnXoa.Enabled = false;
                this.btnSua.Enabled = false;
            }
        }

        private void setDuLieuTextBox(DataRow row)
        {
            if (row != null)
            {
                this.txtTenLop.Text = row[1].ToString();
                this.txtSiSo.Text = row[2].ToString();
                this.lkeKhoiLop.EditValue = row[3].ToString();
            }
        }

        private void btnBoQua_Click(object sender, EventArgs e)
        {
            xoaTrangCacThuocTinh();
            setCacThuocTinh(false);
            this.btnThem.Enabled = this.btnSua.Enabled = true;
            this.btnThem.Text = "Thêm";
            this.btnSua.Text = "Sửa";
            this.btnBoQua.Enabled = false;
            if (dgvLopHoc.DataRowCount != 0)
                dgvLopHoc.SelectRow(1);
        }

        private void dgvLopHoc_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            try
            {
                this.txtSiSo.Text = dgvLopHoc.GetRowCellValue(e.RowHandle, dgvLopHoc.Columns[2]).ToString().Trim();
                this.txtTenLop.Text = dgvLopHoc.GetRowCellValue(e.RowHandle, dgvLopHoc.Columns[1]).ToString().Trim();
                this.lkeKhoiLop.Text = dgvLopHoc.GetRowCellValue(e.RowHandle, dgvLopHoc.Columns[3]).ToString().Trim();
                selected_Lop.TenLop = this.txtTenLop.Text;
                selected_Lop.SiSo = int.Parse(this.txtSiSo.Text);
                selected_Lop.MaKhoiLop = dgvLopHoc.GetRowCellValue(e.RowHandle, dgvLopHoc.Columns["tenKL"]).ToString().Trim();
                selected_Lop.MaLop = dgvLopHoc.GetRowCellValue(e.RowHandle, dgvLopHoc.Columns["malop"]).ToString().Trim();
            }
            catch (Exception ex)
            {

            }
        }

        private void isInteger(string str, string name)
        {
            int value;
            if (!int.TryParse(str, out value))
            {
                XtraMessageBox.Show("Dữ liệu ở ô " + name + " không đúng", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void txtSiSo_Leave(object sender, EventArgs e)
        {
            isInteger(this.txtSiSo.Text, "Sĩ Số");
        }
    }
}
